package cn.qn.service.impl.team;

import cn.qn.base.PageVO;
import cn.qn.dto.team.RecruitDTO;
import cn.qn.dto.team.RecruitQueryDTO;
import cn.qn.enums.BooleanEnum;
import cn.qn.exceptions.BizException;
import cn.qn.mapper.team.RecruitMapper;
import cn.qn.moudle.sys.City;
import cn.qn.moudle.team.Recruit;
import cn.qn.repository.sys.CityRepository;
import cn.qn.repository.team.RecruitRepository;
import cn.qn.service.team.RecruitService;
import cn.qn.utils.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/5/29
 * @description:
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = RecruitService.class)
public class RecruitServiceImpl implements RecruitService {

    @Resource
    private RecruitMapper recruitMapper;
    @Autowired
    private RecruitRepository recruitRepository;
    @Autowired
    private CityRepository cityRepository;
    @Override
    public PageVO<RecruitDTO> pageQueryRecruits(RecruitQueryDTO query) {
        PageBounds pb = new PageBounds(query.getPageBounds().getPage(), query.getPageBounds().getLimit());
        List<RecruitDTO> list = recruitMapper.pageQueryRecruits(query, pb);
        Integer count = recruitMapper.pageQueryRecruitsCount(query);
        return PageVO.convert(pb.getPage(), pb.getLimit(), list, count);
    }

    @Override
    public RecruitDTO queryDetailById(Integer id) {
        RecruitDTO dto=new RecruitDTO();
        Recruit recruit = recruitRepository.findRecruitById(id);
        BeanUtils.copyProperties(recruit,dto);
        dto.setStartTime(DateFormatUtils.format(recruit.getStartTime(),"yyyy-MM-dd"));
        dto.setCreateTime(DateFormatUtils.format(recruit.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
        City city = cityRepository.findCityByCode(recruit.getWorkplace());
        if(null!=city){
            dto.setShowWorkplace(city.getName());
            dto.setProvinceCode(city.getProvinceCode());
        }
        return dto;
    }

    @Override
    public void addRecruit(RecruitDTO dto) throws BizException {
        Recruit recruit=new Recruit();
        BeanUtils.copyProperties(dto,recruit);
        recruit.setStartTime(DateUtils.parseStrToDate(dto.getStartTime(),DateUtils.DATE_FORMAT_YY_MM_DD));
        recruit.setCreateBy(dto.getOperator());
        recruit.setCreateTime(new Date());
        recruit.setStatus(BooleanEnum.TRUE.getCode().toString());
        recruitRepository.save(recruit);
    }

    @Override
    public void editRecruit(RecruitDTO dto)  throws BizException{
        Recruit recruit=recruitRepository.findRecruitById(dto.getId());
        if(recruit==null){
            throw new BizException("该记录不存在");
        }
        BeanUtils.copyProperties(dto,recruit);
        recruit.setStartTime(DateUtils.parseStrToDate(dto.getStartTime(),DateUtils.DATE_FORMAT_YY_MM_DD));
        recruit.setUpdateBy(dto.getOperator());
        recruit.setUpdateTime(new Date());
        recruitRepository.save(recruit);
    }

    @Override
    public void editStatus(RecruitDTO dto) throws BizException{
        Recruit recruit=recruitRepository.findRecruitById(dto.getId());
        if(recruit==null){
            throw new BizException("该记录不存在");
        }
        recruit.setStatus(dto.getStatus());
        recruit.setUpdateBy(dto.getOperator());
        recruit.setUpdateTime(new Date());
        recruitRepository.save(recruit);
    }

    @Override
    public void deleteRecruit(Integer id) {
        recruitRepository.deleteById(id);
    }
}
